$(function() {
    $( "#tabs" ).tabs();

    window.jsonParser = new JsonParser("");

    $("#secretKey").val($("option:selected", $("#selClient")).attr("key"));

    $("#selClient").change(function(){
        var optionSelected = $("option:selected", this);
        $("#secretKey").val($(optionSelected).attr("key"));

        $("#p_clientId").val( $("#selClient").val());
        $("#p_secretKey").val( $("#secretKey").val());
    })

    $("#p_clientId").val( $("#selClient").val());
    $("#p_secretKey").val( $("#secretKey").val());
});

function showResult(settings){
    $.fn.mask();
    $("#requestedUrl").val(  $("#requestedUrl").attr("domain") + settings.url);
    $("#p_data").text(settings.data == null?"":settings.data );
    $.ajax({
        type: settings.type,
        url:settings.url,
        dataType: settings.dataType,
        data: settings.data,
        contentType: "application/json",
        headers:{
            "Client-Id": $("#selClient").val(),
            "Secret-Key" : $("#secretKey").val(),
            "Visitor-Id" : $("#visitorId").val(),
            "Session-Id" : $("#sessionId").val()
        },
        success:function(data){

            window.jsonParser.jsonContent = JSON.stringify(data);;
            window.jsonParser.init();

            if(settings.success)
                settings.success(data);

            $.fn.mask.close();
        },
        error: function(data){
            window.jsonParser.jsonContent = JSON.stringify(data);;
            window.jsonParser.init();
            $.fn.mask.close();
        }
    });
}

var winery ={
    getById : function(){
        var settings = {
            type:"Get",
            url:"/winery/" + $("#wineryId").val(),
            dataType: "json"
        };
        showResult(settings);
    }
};

var nav = {
    getTopNav : function(){
        var settings = {
            type:"Get",
            url:"/topNavigation",
            dataType: "json"
        };
        showResult(settings);
    }
};

var visitor = {
    create: function(){
        var settings = {
            type:"POST",
            url:"/visitor/create",
            dataType: "json",
            success: function(data){
                $("#visitorId").val(data.id);
            }
        };
        showResult(settings);
    }
};

var session ={
    create: function(){
        var settings = {
            type:"POST",
            url:"/session/create",
            dataType: "json",
            success: function(data){
                $("#sessionId").val(data.id);
            }
        };
        showResult(settings);
    }
};

var goods = {
    getById:function(){
        var settings = {
            type:"Get",
            url:"/goods/" + $("#goodsId").val(),
            dataType: "json"
        };
        showResult(settings);
    },
    search:function(){
        var settings = {
            type:"POST",
            url:"/search",
            dataType: "json",
            data:JSON.stringify( {
                "freeText": $("#freeText").val(),
                "country":$("#country").val(),
                "grade":$("#grade").val(),
                "wineryId": $("#winery").val(),
                "ingredient" : $("#ingredient").val(),
                "cate_id_1" : $("#categoryId").val(),
                "order" :  $("#order").val(),
                "priceRange" :[$("#minPrice").val(), $("#maxPrice").val()],
                "new":$("#isNew").is(':checked'),
                "recommended":$("#isRecommended").is(':checked'),
                "hot" : $("#isHot").is(':checked')
            })
        };

        showResult(settings);
    }
};

var country={
    getAll:function(){
        var settings = {
            type:"GET",
            url:"/country",
            dataType: "json"
        };
        showResult(settings);
    }
};

var article = {
    getById:function(){
        var settings = {
            type:"Get",
            url:"/article/" + $("#articleId").val(),
            dataType: "json"
        };
        showResult(settings);
    }
};

var member ={
    login: function(){
        var settings = {
            type:"POST",
            url:"/login",
            dataType: "json",
            data:JSON.stringify( {
                "userName" : $("#userName").val(),
                "password" : $("#password").val()
            })
        };

        showResult(settings);
    },
    logout:function(){
        var settings = {
            type:"GET",
            url:"/logout",
            dataType: "json"
        };

        showResult(settings);
    },
    sendSMS: function(){
        var settings = {
            type:"POST",
            url:"/member/sendSMS",
            dataType: "json",
            data:JSON.stringify( {
                "phone" : $("#c_phone").val()
            })
        };
        showResult(settings);
    },
    create:function(){
        var settings = {
            type:"POST",
            url:"/member/create",
            dataType: "json",
            data:JSON.stringify( {
                "userName" : $("#c_userName").val(),
                "password" : $("#c_password").val(),
                "captcha" : $("#c_captcha").val()
            })
        };

        showResult(settings);
    },
    update:function(){
        var settings = {
            type:"PUT",
            url:"/member/update",
            dataType: "json",
            data:JSON.stringify( {
                "userName" : $("#u_userName").val(),
                "password" : $("#u_password").val(),
                "captcha" : $("#u_captcha").val()
            })
        };

        showResult(settings);
    }
};

var fav={
    show:function(){
        var settings = {
            type:"POST",
            url:"/favorite",
            dataType: "json"
        };
        showResult(settings);
    },
    add:function(){
        var settings={
            type:"POST",
            url:"/favorite/add",
            dataType:"json",
            data:JSON.stringify({
                "goods_id":$("#fav_goods_id").val()
            })
        };
        showResult(settings);
    },
    remove:function(){
        var settings={
            type:"DELETE",
            url:"/favorite/remove/"+$("#fav_id").val(),
            dataType:"json"
        };
        showResult(settings);
    }
};

var cart = {
    shoppingCart:function(){
        var settings = {
            type:"GET",
            url:"/shoppingCart",
            dataType: "json"
        };
        showResult(settings);
    },
    add : function(){
        var settings = {
            type:"POST",
            url:"/cart/add",
            dataType: "json",
            data:JSON.stringify( {
                "goods_id" : $("#c_goodsId").val(),
                "quantity" : $("#c_qty").val(),
                "myListItemId" : $("#c_myListItemId").val()
            })
        };
        showResult(settings);
    },
    delete : function(){
        var settings = {
            type:"Delete",
            url:"/cart/" + $("#c_cartId").val(),
            dataType: "json"
        };
        showResult(settings);
    },
    update : function(){
        var settings = {
            type:"Put",
            url:"/cart/update/",
            dataType: "json",
            data:JSON.stringify( {
                "cartItemId" : $("#c_cartId2").val(),
                "quantity" : $("#c_qty2").val()
            })
        };
        showResult(settings);
    },
    setDeliveryType : function(){
        var settings = {
            type:"Put",
            url:"/cart/setDeliveryType",
            dataType: "json",
            data:JSON.stringify( {
                "deliveryType" : $("#c_shippingType").val()
            })
        };
        showResult(settings);
    },
    checkout : function(){
        var settings = {
            type:"GET",
            url:"/cart/checkout",
            dataType: "json"
        };
        showResult(settings);
    },
    setShippingAddress : function(){

        var settings = {
            type:"PUT",
            url:"/cart/setShippingAddress",
            dataType: "json",
            data:JSON.stringify( {
                "addressId" : $("#c_addressId").val()
            })
        };
        showResult(settings);
    }
};

var address = {
    create: function(){
        var settings = {
            type:"POST",
            url:"/address/create",
            dataType: "json",
            data:JSON.stringify( {
                "contact":$("#a_contact").val(),
                "phone" : $("#a_phone").val(),
                "shippingAddress" : $("#a_shippingAddress").val(),
                "province" : $("#a_province").val(),
                "city" : $("#a_province").val(),
                "county" : $("#a_county").val()
            })
        };
        showResult(settings);
    },
    getMemberAddress:function(){
        var settings = {
            type:"Get",
            url:"/address",
            dataType: "json"
        };
        showResult(settings);
    },
    delete: function(){
        var settings = {
            type:"DELETE",
            url:"/address/" + $("#a_addressId").val(),
            dataType: "json"
        };

        showResult(settings);
    }
};

var order={
    placeOrder: function () {
        var settings={
            type:"GET",
            url:"/order/placeOrder",
            dataType:"json"
        };
        showResult(settings);
    },

    getOrderList:function(){
        var settings = {
            type:"POST",
            url:"/orderHistory",
            dataType: "json",
            data:JSON.stringify( {
                "pageIndex" : $("#o_pageIndex").val(),
                "pageSize" : $("#o_pageSize").val()
            })
        };
        showResult(settings);
    },
    orderDetail: function(){
        var settings = {
            type:"GET",
            url:"/order/" + $("#o_orderId").val(),
            dataType: "json"
        };
        showResult(settings);
    },
    cancelOrder : function(){
        var settings = {
            type:"DELETE",
            url:"/order/" + $("#o_orderId").val(),
            dataType: "json"
        };
        showResult(settings);
    },
    createPayment : function(){
        var settings = {
            type:"POST",
            url:"/orderPayment/create",
            dataType: "json",
            data:JSON.stringify( {
                "orderId" : $("#op_orderId").val(),
                "paymentType" : $("#o_paymentType").val()
            })
        };
        showResult(settings);
    },
    getPaymentForm : function(){
        var settings = {
            type:"GET",
            url:"/alipay/" + $("#op_orderId").val(),
            dataType: "json"
        };
        showResult(settings);
    }
};

var myList = {
    getMyList:function(){
        var settings = {
            type:"POST",
            url:"/myList",
            dataType: "json",
            data:JSON.stringify( {
                "pageIndex" : $("#m_pageIndex").val(),
                "pageSize" : $("#m_pageSize").val(),
                "onSale" : $("#m_onSale").is(':checked')
            })
        };
        showResult(settings);
    },
    getCellar:function(){
        var settings = {
            type:"POST",
            url:"/cellar",
            dataType: "json",
            data:JSON.stringify( {
                "pageIndex" : $("#mc_pageIndex").val(),
                "pageSize" : $("#mc_pageSize").val(),
                "onSale" : $("#mc_onSale").is(':checked'),
                "memberId" : $("#mc_memberId").val()
            })
        };
        showResult(settings);
    },
    getFriend:function(){
        var settings = {
            type:"POST",
            url:"/myList/friend/list",
            dataType: "json",
            data:JSON.stringify( {
                "pageIndex" : $("#mc_pageIndex").val(),
                "pageSize" : $("#mc_pageSize").val()
            })
        };
        showResult(settings);
    },
    getOnSaleItemByPid : function(){
        var settings = {
            type:"GET",
            url:"/myList/p/" + $("#m_pId").val(),
            dataType: "json",
            data:JSON.stringify( {
                "pageIndex" : $("#m_pageIndex").val(),
                "pageSize" : $("#m_pageSize").val()
            })
        };
        showResult(settings);
    },
    getRepurchaseList:function(){
        var settings = {
            type:"POST",
            url:"/myList/repurchase/list",
            dataType: "json",
            data:JSON.stringify( {
                "pageIndex" : $("#m_pageIndex").val(),
                "pageSize" : $("#m_pageSize").val()
            })
        };
        showResult(settings);
    },
    onSale : function(){
        var settings = {
            type:"POST",
            url:"/myList/sale",
            dataType: "json",
            data:JSON.stringify( {
                "onSale" : $("#m_u_onSale").is(':checked'),
                "myListItemId" : $("#m_myListItemId").val(),
                "price" : $("#m_myListItemPrice").val()
            })
        };
        showResult(settings);
    },
    repurchase : function(){
        var settings = {
            type:"POST",
            url:"/myList/repurchase",
            dataType: "json",
            data:JSON.stringify( {
                "qty" : $("#m_myListItemQty").val(),
                "myListId" : $("#m_myListId").val(),
                "myListItemId" : $("#m_myListItemId").val()
            })
        };
        showResult(settings);
    }

};

var recharge = {
    create : function(){
        var settings = {
            type:"POST",
            url:"/recharge/create",
            dataType: "json",
            data:JSON.stringify( {
                "amount" : $("#r_amount").val()
            })
        };
        showResult(settings);
    },
    getList :function(){
        var settings = {
            type:"POST",
            url:"/memberBalanceLog/list",
            dataType: "json",
            data:JSON.stringify( {
                "pageIndex" : $("#r_pageIndex").val(),
                "pageSize" : $("#r_pageSize").val()
            })
        };
        showResult(settings);
    }
};

var friend = {
    searchFriend: function(){
        var settings = {
            type:"POST",
            url:"/friend",
            dataType: "json",
            data:JSON.stringify( {
                "pageIndex" : $("#fm_pageIndex").val(),
                "pageSize" : $("#fm_pageSize").val()
            })
        };
        showResult(settings);
    },
    getList : function(){

        var numberList = [];

        var numberStr = $("#f_numberList").val();
        numberList = numberStr.split(',');

        var settings = {
            type:"POST",
            url:"/member/search",
            dataType: "json",
            data:JSON.stringify( {
                "q" : $("#f_q").val(),
                "pageIndex" : $("#r_pageIndex").val(),
                "pageSize" : $("#r_pageSize").val(),
                "numberList" : numberList
            })
        };
        showResult(settings);
    },
    add: function(){
        var settings = {
            type:"POST",
            url:"/friend/" + $("#f_id").val(),
            dataType: "json"
        };
        showResult(settings);
    },
    remove : function(){
        var settings = {
            type:"DELETE",
            url:"/friend/" + $("#f_id").val(),
            dataType: "json"
        };
        showResult(settings);
    }

};


var message  = {
    getList: function(){
        var settings = {
            type:"get",
            url:"/message",
            dataType: "json"
        };
        showResult(settings);
    }
};

var activity={
    getActivity:function(){
        var settings = {
            type:"post",
            url:"/activity",
            dataType: "json"
        };
        showResult(settings);
    }
};

var thirdPartyLogin ={
    tLogin:function(){
        var settings = {
            type:"post",
            url:"/thirdPartyLogin",
            dataType: "json",
            data:JSON.stringify( {
                "wxId" : $("#thirdPartyId").val()
            })
        };
        showResult(settings);
    },

    tBind:function(){
        var settings = {
            type:"post",
            url:"/thirdPartyLoginBinding",
            dataType: "json",
            data:JSON.stringify( {
                "wxId" : $("#thirdPartyLoginId").val(),
                "userName": $("#thirdPartyUserName").val(),
                "password": $("#thirdPartyPassword").val()
            })
        };
        showResult(settings);
    }
};


var urlHelper = {
    parse: function(){
        var settings = {
            type:"post",
            url:"/urlHelper",
            dataType: "json",
            data:JSON.stringify( {
                "url" : $("#txtUrl").val()
            })
        };
        showResult(settings);
    }
};